// pages/zshAppDetail/zshAppDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        stockData: "",
        defaultPatient: "",
        patientsList: [],
        isBtn: true,
        vanp: false,
        userId: "",
        stockId: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        wx.request({
            url: 'https://118.31.170.229:5001/api/ZshStock/GetStockDetail',
            methodL: 'get',
            data: {
                stockId : options.id
            },
            success: (res) => {
              console.log(res)
              this.setData({
                stockData : res.data
              })
              console.log(this.data.stockData)
            }
          })
          wx.request({
            url: 'https://118.31.170.229:5001/api/ZshPatient/GetAcquiesce',
            methodL: 'get',
            data: {
                userId : options.userId
            },
            success: (res) => {
              console.log(res)
              this.setData({
                defaultPatient : res.data
              })
            //   console.log(this.data.stockData)
            }
          })  
          wx.request({
            url: 'https://118.31.170.229:5001/api/ZshPatient/GetPatients',
            methodL: 'get',
            data: {
                userId : options.userId
            },
            success: (res) => {
              console.log(res)
              this.setData({
                patientsList : res.data
              })
            //   console.log(this.data.stockData)
            }
          })
          this.setData({
            userId : options.userId,
            stockId : options.id
          }) 
    },

    changePatient() {
        this.setData({
          changePatient: false,
          vanp: true
        })
    },

    onCancel() {
      this.setData({
        vanp: false,
        changePatient: true
      })
    },

    onConfirm(item) {
      console.log(item)
      this.setData({
        defaultPatient : item.detail.value,
        vanp: false,
        changePatient: true
      })
    },

    appCancel() {
      wx.navigateBack()
    },

    appConfirm() {
      // console.log(this.data.userId)
      // console.log(this.data.stockId)
      // console.log(this.data.defaultPatient.id)
      let params = {
        userId : parseInt(this.data.userId),
        patientId : parseInt(this.data.defaultPatient.id),
        stockId : parseInt(this.data.stockId)
      }
      console.log(params)
      wx.request({
        url: 'https://118.31.170.229:5001/api/ZshRegister/Add',
        methodL: 'get',
        data : params,
        success: (res) => {
          // console.log(res)
          if(res.data > 0) {
            wx.showToast({ title: '挂号成功', icon: 'success' });
            setTimeout(() => {
              wx.navigateTo({
              url: '../zshAppResult/zshAppResult?id='+res.data+'&stockId='+params.stockId,
            })
            },600)
          }else if(res.data == 0) {
            wx.showToast({ title: '挂号失败', icon: 'error' });
            // Toast.fail('挂号失败，请稍后再试');
          }else if(res.data == -1) {
            wx.showToast({ title: '就诊人已挂号', icon: 'error' });
            // Toast.fail('该就诊人存在未就诊挂号');
          }else if(res.data == -2) {
            wx.showToast({ title: '帐号无挂号资格', icon: 'error' });
            // Toast.fail('在一个月内超过3次爽约，无挂号资格');
          }else {
            wx.showToast({ title: '系统异常', icon: 'error' });
            // Toast.fail('系统异常，请稍后再试');
          }
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})